<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="isAdd">
        <el-input type="password" v-model="dataForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword" v-if="isAdd" ref="confirmPassword">
        <el-input type="password" v-model="dataForm.confirmPassword" @input="change"></el-input>
      </el-form-item>
      
      <el-form-item label="所属机构" prop="orgNo">
        <!-- <el-popover
          ref="orgListPopover"
          placement="bottom-start"if
          trigger="click">
          <el-tree
            :data="orgNoOptions"
            :props="orgListTreeProps"
            node-key="orgNo"
            ref="orgListTree"
            @current-change="orgListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.orgName" v-popover:orgListPopover :readonly="true" placeholder="点击选择上级机构"
                  class="org-list__input"></el-input> -->

        <el-select v-model="dataForm.orgName" placeholder=""  ref="selectReport" style="width: 100%">
          <el-option :value="dataForm.orgNo" :label="dataForm.orgName" style="width:100%;height:200px;max-height:300px;overflow: auto;background-color:#fff">
              <el-tree
                :data="orgNoOptions"
                :props="orgListTreeProps"
                @node-click="handleNodeClick"
              ></el-tree>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" row="2" v-model="dataForm.description" placeholder=""></el-input>
      </el-form-item>
      <!-- <el-form-item label="绑定范围" prop="binding">
        <el-input type="textarea" v-model="dataForm.binding" placeholder="请输入以逗号分隔的ip：例:192.168.34.74,192.168.34.58"></el-input>
      </el-form-item>
      <el-form-item label="排除IP" prop="excludeIp">
        <el-input type="textarea" v-model="dataForm.excludeIp" placeholder="请输入以逗号分隔的ip：例:192.168.34.74,192.168.34.58"></el-input>
      </el-form-item> -->
      <el-form-item label="所属VLAN" prop="vlan">
        <el-input type="textarea" v-model="dataForm.vlan" placeholder="请输入以逗号分隔的vlan：例:1302,1303"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      let validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.password !== value) {
          callback(new Error('确认密码与新密码不一致'))
        } else {
          callback()
        }
      }
      return {
        orgNoOptions: this.treeDataTranslate(JSON.parse(sessionStorage.getItem('orgList') || '[]'), 'orgNo', 'parentNo'),
        orgListTreeProps: {
          label: 'orgName',
          children: 'children'
        },
        isAdd: true,
        visible: false,
        dataForm: {
          orgName: '',
          id: 0,
          userName: '',
          password: '',
          confirmPassword: '',
          orgNo: '',
          status: 1,
          description: '',
          binding: '',
          excludeIp: '',
          vlan: ''
        },
        dataRule: {
          userName: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
          password: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ],
          confirmPassword: [
            {required: true, message: '确认密码不能为空', trigger: 'change'},
            {validator: validateConfirmPassword, trigger: 'change'}
          ]
        }
      }
    },
    methods: {
      change (e) {
        this.$forceUpdate()
      },
      init (id) {
        this.isAdd = true
        this.$nextTick(() => {
          this.visible = true
          this.dataForm.id = id || 0
          this.$http({
            url: '/sys/org/queryAll',
            method: 'get'
          }).then(({data}) => {
            this.orgNoOptions = this.treeDataTranslate(data.list, 'orgNo', 'parentNo')
          }).then(() => {
            this.clearForm()
            if (this.dataForm.id !== 0) {
              this.isAdd = false
              this.$http({
                url: `/sys/user/info/${this.dataForm.id}`,
                method: 'get'
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.user
                  this.dataForm.id = id
                  this.dataForm.userId = data.user.userId
                  this.dataForm.orgName = this.transOrg(data.user.orgNo)
                  delete this.dataForm.password
                }
              })
            }
          })
        })
      },
      clearForm () {
        this.dataForm.orgName = ''
        this.dataForm.password = ''
        this.dataForm.confirmPassword = ''
        this.dataForm.userName = ''
        this.dataForm.orgNo = ''
        this.dataForm.status = 1
        this.dataForm.description = ''
        this.dataForm.excludeIp = ''
        this.dataForm.binding = ''
        this.dataForm.vlan = ''
      },
      // 机构树设置当前选中节点
      orgListTreeSetCurrentNode (type) {
        if (type === 'mod') {
          this.$refs.orgListTree.setCurrentKey(this.dataForm.parentNo)
          this.dataForm.parentName = (this.$refs.orgListTree.getCurrentNode() || {})['orgName']
        }
      },
      // 机构树选中
      orgListTreeCurrentChangeHandle (data) {
        this.dataForm.orgNo = data.orgNo
        this.dataForm.orgName = data.orgName
      },
      handleNodeClick (node) {
        if (node.children) {
        } else {
          this.dataForm.orgNo = node.orgNo
          this.dataForm.orgName = node.orgName
          this.$refs.selectReport.blur()
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `/sys/user/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'userId': this.dataForm.id || undefined,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'orgNo': this.dataForm.orgNo,
                'status': this.dataForm.status,
                'description': this.dataForm.description,
                'binding': this.dataForm.binding,
                'excludeIp': this.dataForm.excludeIp,
                'vlan': this.dataForm.vlan
              }
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  .mod-org {

    .org-list__input,
    .icon-list__input {

      > .el-input__inner {
        cursor: pointer;
      }

    }
    &
    __icon-popover {
      max-width: 350px;
    }

    &
    __icon-list {
      max-height: 380px;
      padding: 0;
      margin: -8px 0 0 -8px;

      > .el-button {
        padding: 8px;
        margin: 8px 0 0 8px;

        > span {
          display: inline-block;
          vertical-align: middle;
          width: 18px;
          height: 18px;
          font-size: 18px;
        }

      }
    }
    .icon-list__tips {
      font-size: 18px;
      text-align: center;
      color: #e6a23c;
      cursor: pointer;
    }

  }
</style>
